package com.baize.imagehandledemo;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.io.File;
import java.io.IOException;

import static android.app.Activity.RESULT_OK;

public class ImageBottomSheetFragment extends BottomSheetDialogFragment {
    public static final int REQUEST_LOCAL_111 = 111;
    public static final int REQUEST_CAMERA_112 = 112;
    public static final int REQUEST_CROP_113 = 113;
    public static final int REQUEST_CROP_SYSTEM_113 = 114;

    private ImageResultListener mListener;
    private boolean mEnableCrop;
    private View contentView;

    public ImageBottomSheetFragment(ImageResultListener mListener) {
        this.mListener = mListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        final View view = getView();
        view.post(new Runnable() {
            @Override
            public void run() {
                View parent = (View) view.getParent();
                if (parent != null) {
                    parent.setBackgroundColor(Color.TRANSPARENT);
                }
            }
        });
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.dialog_header, container, false);
        initView();
        return contentView;
    }

    private void initView() {
        contentView.findViewById(R.id.btn_album).setOnClickListener(mClickListener);
        contentView.findViewById(R.id.btn_camera).setOnClickListener(mClickListener);
        contentView.findViewById(R.id.btn_cancel).setOnClickListener(mClickListener);
    }

    private PerfectClickListener mClickListener = new PerfectClickListener() {
        @Override
        protected void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.btn_album:
                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, REQUEST_LOCAL_111);
                    break;
                case R.id.btn_camera:
                    getImageForCamera();
                    break;
                case R.id.btn_cancel:
                    dismiss();
                    break;
            }
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case REQUEST_LOCAL_111://相册返回
                if (resultCode == RESULT_OK) {
                    Uri uri = data.getData();
                    String filePath = ImageToolUtils.getRealPathFromUriAboveApi19(getContext(), uri);
                    resultOrCrop(filePath);
                }
                break;
            case REQUEST_CAMERA_112://拍照返回
                if (resultCode == RESULT_OK) {
                    String filePath = ImageToolUtils.getCameraCache(getContext()).getAbsolutePath();
                    resultOrCrop(filePath);
                }
                break;
        }
    }

    //拍照
    private void getImageForCamera() {
        File cameraFile = ImageToolUtils.getCameraCache(getContext());
        if (cameraFile.exists()) {
            cameraFile.delete();
        }
        try {
            cameraFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Uri photoUri = ImageToolUtils.getFileUri(getContext(), cameraFile);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(intent, REQUEST_CAMERA_112);
    }

    private void resultOrCrop(String path) {
        if (mEnableCrop) {
            CropImgActivity.start(getActivity(), path);
        } else {
            mListener.resultImagePath(path);
        }
        dismiss();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            mEnableCrop = getArguments().getBoolean("crop");
        }
    }

    public static ImageBottomSheetFragment getInstance(ImageResultListener listener, boolean enableCrop) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("crop", enableCrop);
        ImageBottomSheetFragment dialog = new ImageBottomSheetFragment(listener);
        dialog.setArguments(bundle);
        return dialog;
    }

    public interface ImageResultListener {
        void resultImagePath(String path);
    }

}
